﻿using Basic.WxApiService.WxModel;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.Http;

namespace Basic.WxApiService.WxMiniApi
{
    internal class BaseMiniApi
    {
        public static Result PostJson<Result> (string uri, WxMerToken token, string json) where Result : WxBasicRes
        {
            Uri apiUri = new Uri(string.Concat(uri, "?access_token=", token.Token));
            Result result = HttpHelper.PostJson<Result>(apiUri, json);
            if (result.errcode == 0)
            {
                return result;
            }
            else if (result.errcode == 40014 || result.errcode == 42001)
            {
                token.ResetToken();
                return PostJson<Result>(uri, token, json);
            }
            throw new ErrorException(string.Concat("wx.mini.api.", result.errcode));
        }
        public static byte[] PostJsonByStream (string uri, WxMerToken token, string json)
        {
            Uri apiUri = new Uri(string.Concat(uri, "?access_token=", token.Token));
            HttpResponseRes result = HttpHelper.HttpPost(apiUri, json, new HttpRequestSet(HttpReqType.Json));
            if (result.StatusCode != System.Net.HttpStatusCode.OK)
            {
                throw new ErrorException(string.Concat("http.error.", result.StatusCode));
            }
            else if (result.ContentType.EndsWith("/json"))
            {
                WxBasicRes res = result.Content.Json<WxBasicRes>();
                if (res.errcode == 40014 || res.errcode == 42001)
                {
                    token.ResetToken();
                    return PostJsonByStream(uri, token, json);
                }
                throw new ErrorException(string.Concat("wx.mini.api.", res.errcode));
            }
            return result.SourceBytes;
        }
        public static void PostJson (string uri, WxMerToken token, string json)
        {
            Uri apiUri = new Uri(string.Concat(uri, "?access_token=", token.Token));
            WxBasicRes result = HttpHelper.PostJson<WxBasicRes>(apiUri, json);
            if (result.errcode == 0)
            {
                return;
            }
            else if (result.errcode == 40014 || result.errcode == 42001)
            {
                token.ResetToken();
                _ = PostJson<WxBasicRes>(uri, token, json);
                return;
            }
            throw new ErrorException(string.Concat("wx.mini.api.", result.errcode));
        }
        public static Result Get<Result> (string uri, WxMerToken token, string query) where Result : WxBasicRes
        {
            Uri apiUri = query == null ? new Uri(uri + "?access_token=" + token.Token) : new Uri(string.Format("{0}?access_token={1}&{2}", uri, token.Token, query));
            Result result = HttpHelper.Get<Result>(apiUri);
            if (result.errcode == 0)
            {
                return result;
            }
            else if (result.errcode == 40014 || result.errcode == 42001)
            {
                token.ResetToken();
                return Get<Result>(uri, token, query);
            }
            throw new ErrorException(string.Concat("wx.mini.api.", result.errcode));
        }
        public static Result GetResult<Result> (string uri, WxMerToken token, string query) where Result : WxBasicRes
        {
            Uri apiUri = query == null ? new Uri(uri + "?access_token=" + token.Token) : new Uri(string.Format("{0}?access_token={1}&{2}", uri, token.Token, query));
            Result result = HttpHelper.Get<Result>(apiUri);
            if (result.errcode == 0)
            {
                return result;
            }
            else if (result.errcode == 40014 || result.errcode == 42001)
            {
                token.ResetToken();
                return GetResult<Result>(uri, token, query);
            }
            return result;
        }
    }
}
